package com.supreme.commonboot.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

@EqualsAndHashCode(callSuper = true)
@Data
public class AjaxResp extends JSONObject implements Serializable {

    enum HttpCode {

    }

    public static AjaxResp instance() {
        return new AjaxResp();
    }

    public static AjaxResp instance(int status, String message) {
        return new AjaxResp(status, message);
    }

    public static AjaxResp instance(Object data) {
        return new AjaxResp(data);
    }

    public AjaxResp() {
        put("status", 0);
        put("message", "success");
    }

    public AjaxResp(int status, String message) {
        put("status", status);
        put("message", message);
    }

    public AjaxResp(String message) {
        put("status", 0);
        put("message", message);
    }

    public AjaxResp(Object data) {
        put("status", 0);
        put("message", "success");
        put("data", data);
    }

    public AjaxResp(String message, Object data) {
        put("status", 0);
        put("message", message);
        put("data", data);
    }

    public AjaxResp(int status, String message, Object data) {
        put("status", status);
        put("message", message);
        put("data", data);
    }

    public AjaxResp append(String key, Object val) {
        put(key, val);
        return this;
    }

    public <T> AjaxResp setData(T data) {
        put("data", data);
        return this;
    }

    public Object getData() {
        return get("data");
    }

    public <T> T getData(Class<T> clazz) {
        return getObject("data", clazz);
    }

    public AjaxResp raw() {
        remove("status");
        remove("message");
        return this;
    }
}
